package com.goodving.guli.bt.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.goodving.guli.R;
import com.goodving.guli.activity.ag;
import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.utils.WeightUtil;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.view.BitmapDescriptorFactory;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4005t;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by john on 2015/12/4.
 */
public class WeightGoalSetView extends View implements GestureDetector.OnGestureListener {
    public static final int f11197f = 0;
    public static final int f11198g = 1;
    private static Context f11199h;
    private static int f11200i;
    private static int f11201j;
    private static int f11202k;
    private static int f11203l;
    private static int f11204m;
    private static int f11205n;
    private static int f11206o;
    private static int f11207p;
    private static int f11208q;
    private static int f11209r;
    private static int f11210s;
    private static final String f11211y;
    private ag f11212A;
    private Handler f11213B;
    private int f11214C;
    private int f11215D;
    private int f11216E;
    private int f11217F;
    private int f11218G;
    private int f11219H;
    private String f11220I;
    private int f11221J;
    private int f11222K;
    private List<Float> f11223L;
    private boolean f11224M;
    private int[] f11225N;
    Paint f11226a;
    Paint f11227b;
    Paint f11228c;
    Paint f11229d;
    Paint f11230e;
    private int f11231t;
    private int f11232u;
    private int f11233v;
    private int f11234w;
    private GestureDetector f11235x;
    private float f11236z;

    static {
        f11200i = 50;
        f11201j = 4;
        f11202k = 50;
        f11203l = 25;
        f11204m = f11200i + f11201j;
        f11205n = 60;
        f11206o = 25;
        f11207p = 40;
        f11208q = 30;
        f11209r = 30;
        f11210s = 15;
        f11211y = WeightGoalSetView.class.getSimpleName();
    }

    public WeightGoalSetView(Context context) {
        this(context, null);
    }

    public WeightGoalSetView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.f11231t = f11197f;
        this.f11232u = f11197f;
        this.f11233v = f11197f;
        this.f11234w = f11197f;
        this.f11236z = -1;
        this.f11213B = new Handler();
        this.f11221J = f11197f;
        this.f11223L = new ArrayList();
        this.f11224M = false;
        this.f11225N = new int[]{getResources().getColor(R.color.light_color), getResources().getColor(R.color.normal_color), getResources().getColor(R.color.heavy_color), getResources().getColor(R.color.fat_color)};
        f11199h = context;
        this.f11226a = new Paint();
        this.f11227b = new Paint();
        this.f11228c = new Paint();
        this.f11229d = new Paint();
        this.f11230e = new Paint();
        this.f11235x = new GestureDetector(context, this);
        TypedArray obtainStyledAttributes = f11199h.obtainStyledAttributes(attributeSet, C4005t.WeightGoalSetView);
        this.f11215D = obtainStyledAttributes.getColor(f11197f, getResources().getColor(R.color.light_color));
        this.f11216E = obtainStyledAttributes.getColor(f11198g, getResources().getColor(R.color.normal_color));
        this.f11217F = obtainStyledAttributes.getColor(2, getResources().getColor(R.color.heavy_color));
        this.f11218G = obtainStyledAttributes.getColor(3, getResources().getColor(R.color.fat_color));
        this.f11219H = obtainStyledAttributes.getColor(6, -1);
        this.f11214C = Keeper.readPersonInfo().miliConfig.weightUnit;
        this.f11220I = WeightUtil.m15698a(f11199h, this.f11214C);
        GuliLog.tracePrintInfo(f11211y, "weightUnit " + this.f11220I);
        this.f11221J = obtainStyledAttributes.getInteger(5, f11197f);
        obtainStyledAttributes.recycle();
        m15642a();
    }

    private void m15642a() {
        f11200i = (int) Utils.m8026b(16.6f, f11199h);
        f11201j = (int) Utils.m8026b(1.3f, f11199h);
        f11202k = (int) Utils.m8026b(16.6f, f11199h);
        f11203l = (int) Utils.m8026b(8.3f, f11199h);
        f11204m = (int) Utils.m8026b(18.0f, f11199h);
        f11206o = (int) Utils.m8026b(9.0f, f11199h);
        f11207p = Utils.m7961a(10.3f, f11199h);
        f11208q = Utils.m7961a(10.0f, f11199h);
        f11210s = (int) Utils.m8026b(4.0f, f11199h);
        f11205n = (int) Utils.m8026b(13.3f, f11199h);
    }

    private int m15643b(float f) {
        float f2 = 0.0f;
        GuliLog.tracePrintInfo(f11211y, "calDefaultStartX:" + f);
        if (f == 0.0f) {
            f = ((Float) this.f11223L.get(this.f11223L.size() / 2)).floatValue();
        }
        GuliLog.tracePrintInfo(f11211y, "mDivideBlocks " + this.f11222K);
        int i = f11197f;
        float f3 = 0.0f;
        while (i < this.f11222K) {
            GuliLog.tracePrintInfo(f11211y, "scalelist.geti & scalelist.geti+1 " + this.f11223L.get(i) + " " + this.f11223L.get(i + f11198g));
            if (f >= ((Float) this.f11223L.get(i)).floatValue() && f < ((Float) this.f11223L.get(i + f11198g)).floatValue()) {
                f3 = f - ((Float) this.f11223L.get(i)).floatValue();
                float floatValue = ((Float) this.f11223L.get(i + f11198g)).floatValue() - ((Float) this.f11223L.get(i)).floatValue();
                GuliLog.tracePrintInfo(f11211y, "scaleWeight " + f3 + " totalWeight " + floatValue + "scale/total " + (f3 / floatValue) + " i*i/block " + (((float) i) * (1.0f / ((float) this.f11222K))));
                f3 = (((float) i) * (1.0f / ((float) this.f11222K))) + (((f3 / floatValue) * 1.0f) / ((float) this.f11222K));
            }
            i += f11198g;
        }
        if (f >= ((Float) this.f11223L.get(this.f11222K)).floatValue()) {
            f2 = 1.0f;
        } else if (f >= ((Float) this.f11223L.get(f11197f)).floatValue()) {
            f2 = f3;
        }
        this.f11233v = ((int) (((float) ((this.f11231t - (f11204m * 2)) - (f11205n * 2))) * f2)) + (f11205n + f11204m);
        return this.f11233v;
    }

    private void m15644b() {
        if (this.f11221J == 0) {
            if (this.f11233v < f11204m + f11205n) {
                this.f11233v = f11204m + f11205n;
            } else if (this.f11233v > (this.f11231t - f11204m) - f11205n) {
                this.f11233v = (this.f11231t - f11204m) - f11205n;
            }
        }
        GuliLog.tracePrintInfo(f11211y, "onScroll onTouchX " + this.f11233v);
        invalidate();
    }

    private int m15645c(int i) {
        int mode = MeasureSpec.getMode(i);
        int size = MeasureSpec.getSize(i);
        this.f11231t = size;
        if (mode == 1073741824) {
            return size;
        }
        int paddingLeft = getPaddingLeft() + getPaddingRight();
        return mode == Integer.MIN_VALUE ? Math.min(paddingLeft, size) : paddingLeft;
    }

    private int m15646d(int i) {
        int mode = MeasureSpec.getMode(i);
        int size = MeasureSpec.getSize(i);
        this.f11232u = size;
        if (mode == 1073741824) {
            return size;
        }
        int paddingTop = getPaddingTop() + getPaddingBottom();
        return mode == Integer.MIN_VALUE ? Math.min(paddingTop, size) : paddingTop;
    }

    public void m15647a(float f) {
        GuliLog.tracePrintInfo(f11211y, "from userinfo weight " + f);
        this.f11236z = f;
    }

    public void m15648a(int i) {
        this.f11214C = i;
        invalidate();
    }

    public void m15649a(ag agVar) {
        this.f11212A = agVar;
    }

    public void setScaleRange(float... fArr) { // m15650a
        if (fArr.length <= f11198g || fArr.length > 5) {
            GuliLog.tracePrintInfo(f11211y, " scale length is invalid");
            this.f11222K = 3;
            this.f11223L.clear();
            this.f11223L.add(Float.valueOf(0.0f));
            this.f11223L.add(Float.valueOf(50.0f));
            this.f11223L.add(Float.valueOf(100.0f));
            this.f11223L.add(Float.valueOf(BitmapDescriptorFactory.HUE_MAGENTA));
        } else {
            GuliLog.tracePrintInfo(f11211y, " scale length " + fArr.length);
            this.f11222K = fArr.length - 1;
            this.f11223L.clear();
            for (int i = f11197f; i < fArr.length; i += f11198g) {
                this.f11223L.add(Float.valueOf(fArr[i]));
            }
        }
        invalidate();
    }

    public void m15651b(int i) {
        this.f11221J = i;
        invalidate();
    }

    public boolean onDown(MotionEvent motionEvent) {
        GuliLog.tracePrintInfo(f11211y, "onDown getX " + motionEvent.getX());
        this.f11233v = (int) motionEvent.getX();
        m15644b();
        return true;
    }

    protected void onDraw(Canvas canvas) {
        int i;
        int i2 = f11197f;
        super.onDraw(canvas);
        int i3 = (this.f11231t - (f11204m * 2)) - (f11205n * 2);
        if (this.f11222K == 4) {
            this.f11226a.setColor(this.f11225N[f11197f]);
        } else {
            this.f11226a.setColor(this.f11225N[f11198g]);
        }
        canvas.drawCircle((float) (f11205n + f11204m), (float) (((this.f11232u - f11202k) / 2) + f11203l), (float) f11203l, this.f11226a);
        if (this.f11222K == 4) {
            this.f11226a.setColor(this.f11225N[this.f11222K - 1]);
        } else {
            this.f11226a.setColor(this.f11225N[this.f11222K]);
        }
        canvas.drawCircle((float) ((f11205n + f11204m) + i3), (float) (((this.f11232u - f11202k) / 2) + f11203l), (float) f11203l, this.f11226a);
        for (int i4 = f11197f; i4 < this.f11222K; i4 += f11198g) {
            if (this.f11222K == 4) {
                this.f11226a.setColor(this.f11225N[i4]);
            } else {
                this.f11226a.setColor(this.f11225N[i4 + f11198g]);
            }
            canvas.drawRect((((float) i3) * (((float) i4) / ((float) this.f11222K))) + ((float) (f11205n + f11204m)), (float) ((this.f11232u - f11202k) / 2), (((float) i3) * (((float) (i4 + f11198g)) / ((float) this.f11222K))) + ((float) (f11205n + f11204m)), (float) ((this.f11232u + f11202k) / 2), this.f11226a);
        }
        if (this.f11221J == 0) {
            this.f11228c.setColor(getResources().getColor(R.color.text_color_black));
        } else {
            this.f11228c.setColor(getResources().getColor(R.color.weight_report_white_60));
        }
        this.f11228c.setTextSize((float) f11207p);
        this.f11228c.setAntiAlias(true);
        if (this.f11221J == 0) {
            int i5 = ((f11200i + f11201j) - (f11202k / 2)) + f11201j;
            for (int i6 = f11197f; i6 < this.f11222K - 1; i6 += f11198g) {
                canvas.drawText(((int) (((Float) this.f11223L.get(i6 + f11198g)).floatValue() + 0.0f)) + C3657a.f18328f, (((float) (f11205n + f11204m)) + (((float) i3) * (((float) (i6 + f11198g)) / ((float) this.f11222K)))) - ((float) (((int) this.f11228c.measureText(((int) (((Float) this.f11223L.get(i6 + f11198g)).floatValue() + 0.0f)) + C3657a.f18328f)) / 2)), (float) (((this.f11232u / 2) + f11200i) + i5), this.f11228c);
            }
        } else if (this.f11221J == f11198g) {
            for (i = f11197f; i < this.f11222K - 1; i += f11198g) {
                canvas.drawText(this.f11223L.get(i + f11198g) + C3657a.f18328f, (((float) (f11205n + f11204m)) + (((float) i3) * (((float) (i + f11198g)) / ((float) this.f11222K)))) - ((float) (((int) this.f11228c.measureText(this.f11223L.get(i + f11198g) + C3657a.f18328f)) / 2)), (float) (((this.f11232u / 2) + f11200i) + 5), this.f11228c);
            }
        }
        this.f11229d.setColor(-1);
        this.f11229d.setTextSize((float) Utils.m7961a(11.6f, f11199h));
        for (i = f11197f; i < this.f11222K; i += f11198g) {
            if (this.f11222K == 4) {
                canvas.drawText(getResources().getStringArray(R.array.weight_body_figures)[i], (((float) (f11205n + f11204m)) + (((float) i3) * (((float) i) / ((float) this.f11222K)))) + (((((float) i3) / ((float) this.f11222K)) - this.f11229d.measureText(getResources().getStringArray(R.array.weight_body_figures)[i])) / 2.0f), ((float) (this.f11232u / 2)) + Utils.m8026b(4.0f, f11199h), this.f11229d);
            } else if (this.f11222K != f11198g) {
                canvas.drawText(getResources().getStringArray(R.array.weight_body_figures_for_child)[i + f11198g], (((float) (f11205n + f11204m)) + (((float) i3) * (((float) i) / ((float) this.f11222K)))) + (((((float) i3) / ((float) this.f11222K)) - this.f11229d.measureText(getResources().getStringArray(R.array.weight_body_figures)[i])) / 2.0f), ((float) (this.f11232u / 2)) + Utils.m8026b(4.0f, f11199h), this.f11229d);
            }
        }
        if (this.f11233v == 0) {
            m15643b(this.f11236z);
        } else {
            GuliLog.tracePrintInfo(f11211y, "onTouchX is not 0 " + this.f11233v);
        }
        this.f11227b.setColor(-1);
        if (this.f11221J == f11198g) {
            i = f11197f;
            while (i < this.f11222K) {
                if (((float) this.f11233v) < ((float) f11204m) + (((float) i3) * (((float) i) / ((float) this.f11222K))) || this.f11233v >= f11204m + (((i + f11198g) * i3) / this.f11222K)) {
                    if (this.f11233v == f11204m + i3) {
                        if (this.f11222K == 4) {
                            this.f11227b.setColor(this.f11225N[this.f11222K - 1]);
                        } else {
                            this.f11227b.setColor(this.f11225N[this.f11222K]);
                        }
                    }
                } else if (this.f11222K == 4) {
                    this.f11227b.setColor(this.f11225N[i]);
                } else {
                    this.f11227b.setColor(this.f11225N[i + f11198g]);
                }
                i += f11198g;
            }
            Path path = new Path();
            path.reset();
            path.moveTo((float) this.f11233v, (float) ((this.f11232u - f11202k) / 2));
            path.lineTo((float) (this.f11233v + f11206o), (float) (((this.f11232u - f11202k) / 2) - f11206o));
            path.lineTo((float) (this.f11233v - f11206o), (float) (((this.f11232u - f11202k) / 2) - f11206o));
            path.close();
            canvas.drawPath(path, this.f11227b);
        } else if (this.f11221J == 0) {
            this.f11227b.setAntiAlias(true);
            canvas.drawCircle((float) this.f11233v, (float) this.f11234w, (float) (f11200i + f11201j), this.f11227b);
            GuliLog.tracePrintInfo(f11211y, "onTouchx " + this.f11233v);
            GuliLog.tracePrintInfo(f11211y, "raw " + f11205n + f11204m + i3);
            GuliLog.tracePrintInfo(f11211y, "is == " + (this.f11233v == (f11205n + f11204m) + i3));
            i = f11197f;
            while (i < this.f11222K) {
                if (((float) this.f11233v) < ((float) (f11205n + f11204m)) + (((float) i3) * (((float) i) / ((float) this.f11222K))) || ((float) this.f11233v) >= ((float) (f11205n + f11204m)) + (((float) i3) * (((float) (i + f11198g)) / ((float) this.f11222K)))) {
                    if (this.f11233v == (f11205n + f11204m) + i3) {
                        if (this.f11222K == 4) {
                            this.f11227b.setColor(this.f11225N[this.f11222K - 1]);
                        } else {
                            this.f11227b.setColor(this.f11225N[this.f11222K]);
                        }
                    }
                } else if (this.f11222K == 4) {
                    this.f11227b.setColor(this.f11225N[i]);
                } else {
                    this.f11227b.setColor(this.f11225N[i + f11198g]);
                }
                i += f11198g;
            }
            canvas.drawCircle((float) this.f11233v, (float) this.f11234w, (float) f11200i, this.f11227b);
        }
        this.f11229d.reset();
        this.f11229d.setColor(this.f11219H);
        this.f11229d.setAntiAlias(true);
        if (this.f11221J == 0) {
            f11208q = Utils.m7961a(16.0f, f11199h);
        } else if (this.f11221J == f11198g) {
            f11208q = Utils.m7961a(16.0f, f11199h);
        }
        this.f11229d.setTextSize((float) f11208q);
        this.f11230e.reset();
        this.f11230e.setColor(this.f11219H);
        this.f11230e.setAntiAlias(true);
        this.f11230e.setTextSize((float) f11209r);
        float f = ((float) ((this.f11233v - f11204m) - f11205n)) / ((float) i3);
        GuliLog.tracePrintInfo(f11211y, "onTouch " + this.f11233v + " bgVisibleWidth is " + i3 + " percent is " + f);
        GuliLog.tracePrintInfo(f11211y, "percent in int " + Float.floatToIntBits(f));
        GuliLog.tracePrintInfo(f11211y, "1.0f in int " + Float.floatToIntBits(1.0f));
        GuliLog.tracePrintInfo(f11211y, "is == " + (Float.floatToIntBits(f) == Float.floatToIntBits(1.0f)));
        float f2 = 0.0f;
        while (i2 < this.f11222K) {
            if (f >= ((float) i2) / ((float) this.f11222K) && f < ((float) (i2 + f11198g)) / ((float) this.f11222K)) {
                f2 = (((((Float) this.f11223L.get(i2 + f11198g)).floatValue() - ((Float) this.f11223L.get(i2)).floatValue()) * (f - (((float) i2) / ((float) this.f11222K)))) * ((float) this.f11222K)) + ((Float) this.f11223L.get(i2)).floatValue();
            } else if (Float.floatToIntBits(f) == Float.floatToIntBits(1.0f)) {
                f2 = ((Float) this.f11223L.get(this.f11222K)).floatValue();
            }
            i2 += f11198g;
        }
        f2 = ((float) Math.round(f2 * 10.0f)) / 10.0f;
        if (this.f11212A != null) {
            this.f11212A.m15636a((float) ((int) f2));
        }
        if (this.f11221J == f11198g) {
            canvas.drawText(this.f11236z + C3657a.f18328f, ((float) this.f11233v) - (this.f11229d.measureText(this.f11236z + C3657a.f18328f) / 2.0f), ((float) ((((this.f11232u / 2) - f11200i) - f11201j) - f11210s)) - Utils.m8026b(3.3f, f11199h), this.f11229d);
            return;
        }
        GuliLog.tracePrintInfo(f11211y, "weight caculated from onTouchX  is " + f2 + " initWeight is " + this.f11236z);
        if (this.f11224M) {
            canvas.drawText(((int) f2) + C3657a.f18328f, ((float) this.f11233v) - (this.f11229d.measureText(((int) f2) + C3657a.f18328f) / 2.0f), ((float) ((((this.f11232u / 2) - f11200i) - f11201j) - f11210s)) - Utils.m8026b(6.6f, f11199h), this.f11229d);
            canvas.drawText(this.f11220I, ((this.f11229d.measureText(((int) f2) + C3657a.f18328f) / 2.0f) + ((float) this.f11233v)) + Utils.m8026b(3.3f, f11199h), ((float) ((((this.f11232u / 2) - f11200i) - f11201j) - f11210s)) - Utils.m8026b(6.6f, f11199h), this.f11230e);
            return;
        }
        canvas.drawText(this.f11236z + C3657a.f18328f, ((float) this.f11233v) - (this.f11229d.measureText(this.f11236z + C3657a.f18328f) / 2.0f), ((float) ((((this.f11232u / 2) - f11200i) - f11201j) - f11210s)) - Utils.m8026b(6.6f, f11199h), this.f11229d);
        canvas.drawText(this.f11220I + C3657a.f18328f, (((float) this.f11233v) + (this.f11229d.measureText(this.f11236z + C3657a.f18328f) / 2.0f)) + Utils.m8026b(3.3f, f11199h), ((float) ((((this.f11232u / 2) - f11200i) - f11201j) - f11210s)) - Utils.m8026b(6.6f, f11199h), this.f11230e);
    }

    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
        GuliLog.tracePrintInfo(f11211y, "onFling");
        return false;
    }

    public void onLongPress(MotionEvent motionEvent) {
    }

    protected void onMeasure(int i, int i2) {
        setMeasuredDimension(m15645c(i), m15646d(i2));
        GuliLog.tracePrintInfo(f11211y, "onMeasure width " + this.f11231t + " and heght " + this.f11232u);
        this.f11234w = this.f11232u / 2;
    }

    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
        this.f11233v = (int) motionEvent2.getX();
        m15644b();
        return false;
    }

    public void onShowPress(MotionEvent motionEvent) {
    }

    public boolean onSingleTapUp(MotionEvent motionEvent) {
        GuliLog.tracePrintInfo(f11211y, "onSingleTapUp e.getX " + motionEvent.getX());
        this.f11233v = (int) motionEvent.getX();
        m15644b();
        return false;
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        this.f11224M = true;
        return this.f11221J == 0 ? this.f11235x.onTouchEvent(motionEvent) : true;
    }
}
